from app.view_models.book import BookViewModel


class WishViewModel(object):
    def __init__(self, wishs, gift_list):
        self.wishs = []
        self.__wishs = wishs
        self.__gift_list = gift_list
        self.wishs = self.__parse()

    def __parse(self):
        # 这里不去直接操作self.gifts,保证类的稳定性,尽量不要再方法内部修改实例属性.
        temp_list = []
        for wish in self.__wishs:
            my_wish = self.__matching(wish)
            temp_list.append(my_wish)
        return temp_list

    def __matching(self, wish):
        # 这个函数的作用只是让__parse方法下不用写嵌套循环,把一部分循环的代码放到__matching这个函数中,让代码更优美
        count = 0
        for gift in self.__gift_list:
            if wish.isbn == gift['isbn']:
                count = gift['count']
        ret = {
            'id': wish.id,
            'book': BookViewModel(wish.book),
            'wishes_count': count
        }
        return ret